import os
import json
from .. import db
from ..models import UserInfo,Courses,Scores

def save_userinfo(result):
    json_result = json.loads(result)
    print(json_result.get('code'))

    data = json_result.get('result')[0]

    userinfo = UserInfo(uid=data.get('uid'),
                        name=data.get('name'),
                        grade=data.get('grade'),
                        college=data.get('college'),
                        specialty=data.get('specialty'))

    db.session.add(userinfo)
    db.session.commit()

def query_userinfo(uid):
    userinfo = Userinfo.query.filter_by(uid=uid).first()
    if userinfo == None:
        return None
    else:
        return userinfo


# 保存课表到数据库,保存之前先查询是否已经有数据了，如果已经有了，则先删除再存
# TODO 后期可以考虑从这里入手做更新提醒，比如有新增的数据，则标记出来，提示用户有新增的数据
def save_courses(uid,json_list):
    courses = Courses.query.filter_by(uid=uid).all()
    print('courses size:',len(courses))
    if len(courses) > 0:
        for item in courses:
            db.session.delete(item)
        db.session.commit()
    for item in json_list:
        course = Courses(uid=item.get('uid'),
                        schoolYear=item.get('schoolYear'),
                        term=item.get('term'),
                        cid=item.get('cid'),
                        cname=item.get('cname'),
                        period=item.get('period'),
                        credit=item.get('credit'),
                        startSection=item.get('startSection'),
                        endSection=item.get('endSection'),
                        startWeek=item.get('startWeek'),
                        endWeek=item.get('endWeek'),
                        dayOfWeek=item.get('dayOfWeek'),
                        classroom=item.get('classroom'),
                        teacher=item.get('teacher'),
                        campus=item.get('campus'),
                        studyType=item.get('studyType'),
                        studyMethod=item.get('studyMethod'))
        db.session.add(course)
        db.session.commit()

# 保存成绩信息到数据库
def save_scores(uid,json_list):
    scores = Scores.query.filter_by(uid=uid).all()
    if len(scores) > 0:
        for item in scores:
            db.session.delete(item)
        db.session.commit()

    for item in json_list:
        score = Scores(uid=item.get('uid'),
                        schoolYear=item.get('schoolYear'),
                        term=item.get('term'),
                        cid=item.get('cid'),
                        cname=item.get('cname'),
                        type=item.get('type'),
                        property=item.get('property'),
                        credit=item.get('credit'),
                        score=item.get('score'),
                        studyMethod=item.get('studyMethod'))

        db.session.add(score)
        db.session.commit()



# 读取新版本信息
def read_version(device,versionCode):
    basedir=os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir))

    with open(basedir+'/version.json','r') as version:
        json_data = json.loads(version.read())
        if device == 'ios':# iOS
            ios = json_data.get('ios')

            if versionCode < ios.get('versionCode'):# has new versiion
                result_dict = {'hasNew':1,'versionCode':ios.get('versionCode'),'versionName':ios.get('versionName'),'description':ios.get('description'),'url':ios.get('url')}
            else:
                result_dict = {'hasNew':0,'versionCode':'','versionName':'','description':'','url':''}

            return result_dict
        else:# android
            android = json_data.get('android')

            if versionCode < android.get('versionCode'):# has new versiion
                result_dict = {'hasNew':1,'versionCode':android.get('versionCode'),'versionName':android.get('versionName'),'description':android.get('description'),'url':android.get('url')}
            else:
                result_dict = {'hasNew':0,'versionCode':0,'versionName':'','description':'','url':''}

            return result_dict
